<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false" style="padding-bottom: 30px">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billNo">
              <a-input disabled v-model="model.billNo" placeholder="请输入单据编号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="type==1">
            <a-form-model-item label="证书类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="certNo">
              <j-search-select-tag :disabled="formDisabled" placeholder="请选择证书类别" v-model="model.certNo" dict="certificate_type"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="type==2">
            <a-form-model-item label="证书名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="certName">
              <a-input :disabled="formDisabled" v-model="model.certName" placeholder="请输入证书名称" :max-length="100"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="type==3">
            <a-form-model-item label="证书字段" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="certField">
              <a-input :disabled="formDisabled" v-model="model.certField" placeholder="请输入证书字段" :max-length="30"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea :rows="4" :disabled="formDisabled" :max-length="200" v-model="model.remark" placeholder="请输入备注"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { mapGetters } from 'vuex'
export default {
  name: 'ErpCertificateBasicFieldsForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      confirmLoading: false,
      validatorRules: {
        certField: [{ required: true, message: '请输入证书字段!', trigger: 'blur' }],
      },
      url: {
        add: '/api/erp/basecertinfo/field/add',
        edit: '/api/erp/basecertinfo/field/edit',
        queryById: '/api/erp/basecertinfo/field/queryById',
        getBillNo: '/api/erp/basecertinfo/field/billno'
      },
      type: 1
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    ...mapGetters(['userInfo']),
    changeType(billno){
      if(billno.substring(0,4)=='ZSLB'){
        this.type = 1
        this.validatorRules = {
          certNo: [{ required: true, message: '请选择证书类别!', trigger: 'blur' }],
        }
      }
      if(billno.substring(0,4)=='ZSMC'){
        this.type = 2
        this.validatorRules = {
          certName: [{ required: true, message: '请选择证书名称!', trigger: 'blur' }],
        }
      }
      if(billno.substring(0,4)=='ZSZD'){
        this.type = 3
        this.validatorRules = {
          certField: [{ required: true, message: '请输入证书字段!', trigger: 'blur' }],
        }
      }
    },
    add(record) {
      this.$refs['form'].clearValidate()
      this.model = Object.assign({}, {})
      if(record.billNo){
        this.model.parentId = record.id
        if(record.billNo.substring(0,4)=='ZSLB'){
          this.changeType('ZSMC')
          this.getBillNoData('zsmc')
        }
        if(record.billNo.substring(0,4)=='ZSMC'){
          this.changeType('ZSZD')
          this.getBillNoData('zszd')
        }
      } else {
        this.changeType('ZSLB')
        this.getBillNoData('zslb')
      }
      this.visible = true
    },
    getBillNoData(type){
      getAction(this.url.getBillNo,{ type }).then(res => {
        if(res.success){
          this.$set(this.model,'billNo',res.result)
        }
      })
    },
    edit(record) {
      this.$refs['form'].clearValidate()
      this.model = Object.assign({}, record)
      this.changeType(record.billNo)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
                that.$emit('unButtonDisable')
              }
            })
            .catch((error) => {
              that.$emit('unButtonDisable')
            })
            .finally(() => {
              that.confirmLoading = false
            })
        } else {
          that.$emit('unButtonDisable')
        }
      })
    },
  },
}
</script>
